﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class _ISYC_309_02
    {
        public static void  ISYC_309_02(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double scale = 3; //放大系数

            double h = Convert.ToDouble(data["h"]);
            double b = Convert.ToDouble(data["b"]);
            double l3 = Pus.GetLByIC2(Convert.ToDouble(data["ic"]), 3);
            double wf = Convert.ToDouble(data["wf"]);
            double wta = wf - b / 2;
            double lf = Convert.ToDouble(data["lf"]);
            ///////////////////////
            //坐标
            Vector2 v63 = new Vector2(0, 0);
            Vector2 v1 = new Vector2(v63.X - 3, v63.Y - b / 2);
            Vector2 v2 = new Vector2(v1.X + lf, v1.Y);
            Vector2 v64 = new Vector2(v63.X + lf / 1.107, v63.Y);
            Vector2 v65 = new Vector2(v63.X + lf - l3 / 4.232, v64.Y - b / 3.818);
            Vector2 v66 = new Vector2(v63.X + lf, v64.Y - b / 2 - wta);
            Vector2 v85 = fun.Polar(v66, 148 * pi / 180, l3 / 7.529);
            Vector2 v67 = fun.Polar(v85, 90 * pi / 180, l3 / 10.971);
            Vector2 v68 = fun.Polar(v67, 118 * pi / 180, l3 / 1.494);
            Vector2 v69 = fun.Polar(v68, -174 * pi / 180, l3 / 9.846);
            Vector2 v70 = fun.Polar(v69, 131 * pi / 180, l3 / 10.971);
            Vector2 v71 = fun.Polar(v70, -103 * pi / 180, l3 / 6.508);
            Vector2 v72 = fun.Polar(v71, -150 * pi / 180, l3 / 7.111);
            Vector2 v73 = fun.Polar(v72, -123 * pi / 180, l3 / 1.58);
            Vector2 v74 = fun.Polar(v73, -56 * pi / 180, l3 / 16.69);
            Vector2 v75 = fun.Polar(v74, -73 * pi / 180, l3 / 6.19);
            Vector2 v76 = fun.Polar(v75, 37 * pi / 180, l3 / 7.111);
            Vector2 v79 = fun.Polar(v66, -145 * pi / 180, l3 / 7.245);
            Vector2 v78 = fun.Polar(v79, -117 * pi / 180, l3 / 17.454);
            Vector2 v77 = new Vector2(v78.X - l3 / 1.524, v78.Y);
            Vector2 v80 = fun.Polar(v66, -55 * pi / 180, (v66.Y - v78.Y) / Math.Cos(35 * pi / 180));
            Vector2 v81 = new Vector2(v63.X, v63.Y - b);
            Vector2 v82 = new Vector2(v63.X + lf / 1.353, v81.Y);
            Vector2 v83 = new Vector2(v82.X, v74.Y);
            Vector2 v84 = new Vector2(v65.X, v67.Y + (v67.X - v65.X) / Math.Tan(28 * pi / 180));
            Vector2 v86 = new Vector2(v73.X + l3 / 1.855, v77.Y + l3 / 3.048);
           // Vector2 v87 = new Vector2(v74.X + (v74.Y - v83.Y) * Math.Tan(3 * pi / 180), v74.Y);

            Vector2 a63 = fun.mirrorpoint(v63, v1, v2);
            Vector2 a64 = fun.mirrorpoint(v64, v1, v2);
            Vector2 a65 = fun.mirrorpoint(v65, v1, v2);
            Vector2 a66 = fun.mirrorpoint(v66, v1, v2);
            Vector2 a67 = fun.mirrorpoint(v67, v1, v2);
            Vector2 a68 = fun.mirrorpoint(v68, v1, v2);
            Vector2 a69 = fun.mirrorpoint(v69, v1, v2);
            Vector2 a70 = fun.mirrorpoint(v70, v1, v2);
            Vector2 a71 = fun.mirrorpoint(v71, v1, v2);
            Vector2 a72 = fun.mirrorpoint(v72, v1, v2);
            Vector2 a73 = fun.mirrorpoint(v73, v1, v2);
            Vector2 a74 = fun.mirrorpoint(v74, v1, v2);
            Vector2 a75 = fun.mirrorpoint(v75, v1, v2);
            Vector2 a76 = fun.mirrorpoint(v76, v1, v2);
            Vector2 a77 = fun.mirrorpoint(v77, v1, v2);
            Vector2 a78 = fun.mirrorpoint(v78, v1, v2);
            Vector2 a79 = fun.mirrorpoint(v79, v1, v2);
            Vector2 a80 = fun.mirrorpoint(v80, v1, v2);
            Vector2 a81 = fun.mirrorpoint(v81, v1, v2);
            Vector2 a82 = fun.mirrorpoint(v82, v1, v2);
            Vector2 a83 = fun.mirrorpoint(v83, v1, v2);
            Vector2 a84 = fun.mirrorpoint(v84, v1, v2);
            Vector2 a85 = fun.mirrorpoint(v85, v1, v2);
            Vector2 a86 = fun.mirrorpoint(v86, v1, v2);
            //Vector2 a87 = fun.mirrorpoint(v87, v1, v2);
            Vector2 a88 = new Vector2(a63.X + lf / 6.068, a63.Y);
            Vector2 a89 = new Vector2(a88.X + h, a88.Y);
            Vector2 a90 = new Vector2(a89.X, a81.Y);
            Vector2 a91 = new Vector2(a88.X, a90.Y);


            //作图
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(a84));
            line2.Vertexes.Add(new LwPolylineVertex(a65));
            line2.Vertexes.Add(new LwPolylineVertex(a64));
            line2.Vertexes.Add(new LwPolylineVertex(a63));
            line2.Vertexes.Add(new LwPolylineVertex(a81));
            line2.Vertexes.Add(new LwPolylineVertex(a82));
            line2.Vertexes.Add(new LwPolylineVertex(a83));
            line2.Vertexes.Add(new LwPolylineVertex(a74));
            fun.PolyFill(dxf, line2, 0.5, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(a66));
            line3.Vertexes.Add(new LwPolylineVertex(a85));
            line3.Vertexes.Add(new LwPolylineVertex(a67));
            line3.Vertexes.Add(new LwPolylineVertex(a68));
            line3.Vertexes.Add(new LwPolylineVertex(a69));
            line3.Vertexes.Add(new LwPolylineVertex(a70));
            line3.Vertexes.Add(new LwPolylineVertex(a71));
            line3.Vertexes.Add(new LwPolylineVertex(a72));
            line3.Vertexes.Add(new LwPolylineVertex(a73));
            line3.Vertexes.Add(new LwPolylineVertex(a74));
            line3.Vertexes.Add(new LwPolylineVertex(a75));
            line3.Vertexes.Add(new LwPolylineVertex(a76));
            line3.Vertexes.Add(new LwPolylineVertex(a77));
            line3.Vertexes.Add(new LwPolylineVertex(a78));
            line3.Vertexes.Add(new LwPolylineVertex(a79));
            line3.Vertexes.Add(new LwPolylineVertex(a66));
            fun.PolyFill(dxf, line3, 0.5, contour);

         /*   LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(a67));
            line4.Vertexes.Add(new LwPolylineVertex(a66));
            line4.Vertexes.Add(new LwPolylineVertex(a80));
            line4.Vertexes.Add(new LwPolylineVertex(a78));
            fun.PolyFill(dxf, line4, 0.5, contour);*/

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(a88));
            line5.Vertexes.Add(new LwPolylineVertex(a89));
            line5.Vertexes.Add(new LwPolylineVertex(a90));
            line5.Vertexes.Add(new LwPolylineVertex(a91));
            line5.Vertexes.Add(new LwPolylineVertex(a88));
            fun.PolyFill(dxf, line5, 0.5, contour);

            fun.drawcircle(dxf, a86, l3 / 10.564, 0.5, contour);


            //填充
            LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(a88));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(a89));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(a90));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(a91));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(a88));
            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = hatchpoly1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)hatchpoly1.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    harc1[i] = (Arc)hatchpoly1.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 45, 10, hline1, harc1, hatching);


            //标注

            double height = 2;
            double arrowsize = 2;
            double exline = 2;

            fun.dimrot1(dxf, a63, a81, 90, l3 / 2, 1, height, arrowsize, exline, "B", "", "");

            fun.dimrot1(dxf, a81, a66, 0, l3, 1, height, arrowsize, exline, "LF", "", "");

          //  fun.dimrot1(dxf, a66, a66, 0, l3 / 2, 1, height, arrowsize, exline, "PDY", "", "");

          //  fun.dimrot1(dxf, a66, a66, -90, l3 , 1, height, arrowsize, exline, "PDX", "", "");

            fun.dimrot1(dxf, a64, a66, -90, l3 * 5, 1, height, arrowsize, exline, "WF", "", "");

            fun.dimrot1(dxf, a83, a66, 0, l3 * 3, 1, height, arrowsize, exline, "LH", "", "");

            fun.dimrot1(dxf, a88, a89, 180, l3, 1, height, arrowsize, exline, "H", "", "");

            fun.jdbz2(dxf, new Line(a66, fun.Polar(a66, -90 * pi / 180, 0.1)), new Line(a66, fun.Polar(a66, 0 * pi / 180, 0.1)), l3 * 3, height, arrowsize, exline, "KAPR", "", "");



            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }


            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}
